<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Users extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('user');
       
    }

    function index() {
	
	echo "Users index";
           // isloged();
        // we check if there are a  user connected
    }

    function login() {
         
        $this->template
                ->set_layout('login')
                ->build('welcome_message');
    }
 function logout() {
            $this->session->unset_userdata('logged_in');
            redirect('project/add', 'refresh');

    }
    function add() {
           isloged();
        $meta = array(
            'plugins/validate/jquery.validate-min',
            'plugins/autosize/jquery.autosize.min',
            'custom-plugins/wizard/jquery.form.min',
            'js/demo/demo.formelements',
        );
         foreach($meta as $l)
            $this->template->append_metadata(js($l));
         
        $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'projects/addusers')
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
    }

    function adduser() {
        isloged();
        $firstname = $this->input->post('firstname');
        $secandname = $this->input->post('secandname');
        $username = $this->input->post('username');
        $mail = $this->input->post('mailField');
        $password = $this->input->post('passwordField');
        $role = $this->input->post('role');
        
        $result = $this->user->add($firstname,$secandname,$username,$mail,$password,$role);
         $allusers=$this->user->selectAll();
         $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'projects/successuseradd',array("allusers"=>$allusers))
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
         
    }
    function userlist(){
           isloged();
        $meta = array(
            'plugins/datatables/jquery.dataTables.min',
            'js/demo/demo.dashboard',
    );
         foreach($meta as $l)
            $this->template->append_metadata(js($l));
         
         $allusers=$this->user->selectAll();
         $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'projects/successuseradd',array("allusers"=>$allusers))
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
    }
    
    function delete(){
           isloged();
         $user_id = $this->input->get('id');
         $this->user->delete($user_id);
         $allusers=$this->user->selectAll();
         $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'projects/successuseradd',array("allusers"=>$allusers))
                ->set_partial('footer', 'footer')
                ->build('welcome_message'); 
    }
    function update(){
           isloged();
         $user_id = $this->input->get('id');
         $infouser=$this->user->getToUpdate($user_id);
        // 
        
         $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'users/updateUser',array("infouser"=>$infouser))
                ->set_partial('footer', 'footer')
                ->build('welcome_message'); 
    }
    
    function profil(){
           isloged();
        $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'users/profil')
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
    }
    
    function  changepwd(){
           isloged();
          $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'users/profil')
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
    }
    function verifylogin(){
           isloged();
             $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'users/profil')
                ->set_partial('footer', 'footer')
                ->build('welcome_message');  
    }
    function upadateuser(){
           isloged();
         $user_id = $this->input->get('id');
         $this->user->updateUser($user_id);
           $meta = array(
            'plugins/datatables/jquery.dataTables.min',
            'js/demo/demo.dashboard',
    );
         foreach($meta as $l)
            $this->template->append_metadata(js($l));
         
         $allusers=$this->user->selectAll();
         $this->template->set_partial('left_menu', 'left_menu')
                ->set_partial('main_container', 'projects/successuseradd',array("allusers"=>$allusers))
                ->set_partial('footer', 'footer')
                ->build('welcome_message');
    }
    
    
    
}
